<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/res/css/amazeui.min.css}" />
    <link rel="stylesheet" th:href="@{/res/css/admin.css}" />
    <script th:src="@{/js/jquery-1.11.3.min.js}"></script>
    <script th:src="@{/myplugs/js/plugs.js}"></script>
</head>

<style>
    .block{
        width: 400px;
        display: block;
        margin:15px 0;
    }
    .center{
        text-align: center;
    }
    label {
        display: inline-block;
        width: 100px;
        text-align: right;
    }
    input{
        vertical-align: top;
    }
    form{
        position:relative;
        left:35%;
        margin-top: 8%;
    }
    select{
        width: 180px;
    }
    #add{
        margin-right: 2%;
    }
</style>

<body>
<div class="admin-content-body">
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">添加到商店</strong><small></small></div>
    </div>

    <hr>

    <form class="">
        <fieldset>

            <!--<div class="block">
                <label for="number">数量</label>
                <input type="text" class="" id="number">
            </div>-->

            <div class="block">
                <label for="name">店铺名</label>
                <select id="name"></select>
                <span class="am-form-caret"></span>
            </div>

            <div class="block center">
                <button id="add" class="am-btn am-btn-primary am-round" onclick="addGoods()">添加</button>
            </div>
        </fieldset>
    </form>
</div>
</body>
<script>
    //gid
    var result=new Object();
    result=getParameter();
    var gid=result["gid"];
    var uid=result["uid"];

    $.ajax({
        type:'POST',
        url:'/store/getStoreList',
        data:{uid:uid},
        dataType:'json',
        success:function (mydata) {
            console.log(mydata);
            showData(mydata);
        },
        error:function () {
            alert("error");
        }
    });

    function showData(data) {
        var str= "";
        for(var i = 0; i <data.length; i++){
            str = "<option value="+ data[i].id + ">" + data[i].name + "</option>";
            $("#name").append(str);
        }
    };

    function addGoods(){
        var storeId = $("#name").val();
        console.log(storeId);
        $.ajax({
            type: "POST",
            url: "/goods/goodsToStore",
            dataType: "text",
            data:{sid: storeId,gid: gid},
            success:function (data) {
                alert(data);
            },
            error:function (data) {
                alert(data);
            }
        })
        window.location.href="/getGoodList?uid="+uid;
        window.event.returnValue=false;
    };

    /*获取URL*/
    function getParameter(){
        var url = location.search;
        var theRequest = new Object();
        if(url.indexOf("?") != -1){
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i <strs.length; i++){
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
</script>
</html>
